'''
    Created on May 15, 2010

    @author: Samuel Mendenhall (Samuel.Mendenhall@gmail.com)

    This file is part of clusterconfig.py

    Foobar is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Foobar is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
'''
from clusterconfig import Utility, ClusterConfig
from datetime import datetime
from subprocess import Popen, PIPE, STDOUT
import os
import sys
import time
import traceback
import unittest


t = datetime.now()
timestamp = t.strftime("%Y-%m-%d_%H-%M-%S")
dirCurrent = os.path.realpath(os.curdir)
dirArchives = '/synology/archives'

##################################################################################
# mod_proxy RHEL 64-bit
##################################################################################
rhel64ModProxyOneInstanceEap501 = ['--jboss-version', '5.0.1', 
    '--jboss-type', 'Enterprise', 
    '--jboss-instances', '1', 
    '--ip-addresses', '127.0.0.1', 
    '--httpd-version', '2.2.15', 
    '--loadbalancer', 'mod_proxy', 
    '--architecture', 'rhel5_64', 
    '--sandbox-dir-name', 'testSandbox',
    '--archives-dir', dirArchives]

rhel64ModProxyTwoInstancesEap501 = ['--jboss-version', '5.0.1', 
    '--jboss-type', 'Enterprise', 
    '--jboss-instances', '2', 
    '--ip-addresses', '127.0.0.1,127.0.0.2', 
    '--httpd-version', '2.2.15', 
    '--loadbalancer', 'mod_proxy', 
    '--architecture', 'rhel5_64', 
    '--sandbox-dir-name', 'testSandbox',
    '--archives-dir', dirArchives]

rhel64ModProxyOneInstanceEap500 = ['--jboss-version', '5.0.0', 
    '--jboss-type', 'Enterprise', 
    '--jboss-instances', '1', 
    '--ip-addresses', '127.0.0.1', 
    '--httpd-version', '2.2.15', 
    '--loadbalancer', 'mod_proxy', 
    '--architecture', 'rhel5_64', 
    '--sandbox-dir-name', 'testSandbox',
    '--archives-dir', dirArchives]

rhel64ModProxyTwoInstancesEap500 = ['--jboss-version', '5.0.0', 
    '--jboss-type', 'Enterprise', 
    '--jboss-instances', '2', 
    '--ip-addresses', '127.0.0.1,127.0.0.2', 
    '--httpd-version', '2.2.15', 
    '--loadbalancer', 'mod_proxy', 
    '--architecture', 'rhel5_64', 
    '--sandbox-dir-name', 'testSandbox',
    '--archives-dir', dirArchives]



rhel64ModProxySuite = [rhel64ModProxyOneInstanceEap501,
                    rhel64ModProxyTwoInstancesEap501,
                    rhel64ModProxyOneInstanceEap500, 
                    rhel64ModProxyTwoInstancesEap500]

##################################################################################
# mod_jk RHEL 64-bit
##################################################################################
rhel64ModJkOneInstanceEap501 = ['--jboss-version', '5.0.1', 
    '--jboss-type', 'Enterprise', 
    '--jboss-instances', '1', 
    '--ip-addresses', '127.0.0.1', 
    '--httpd-version', '2.2.15', 
    '--loadbalancer', 'mod_jk', 
    '--architecture', 'rhel5_64', 
    '--modjk-version', '1.2.30',
    '--sandbox-dir-name', 'testSandbox',
    '--archives-dir', dirArchives]

rhel64ModJkTwoInstancesEap501 = ['--jboss-version', '5.0.1', 
    '--jboss-type', 'Enterprise', 
    '--jboss-instances', '2', 
    '--ip-addresses', '127.0.0.1,127.0.0.2', 
    '--httpd-version', '2.2.15', 
    '--loadbalancer', 'mod_jk', 
    '--architecture', 'rhel5_64', 
    '--modjk-version', '1.2.30',
    '--sandbox-dir-name', 'testSandbox',
    '--archives-dir', dirArchives]

rhel64ModJkOneInstanceEap500 = ['--jboss-version', '5.0.0', 
    '--jboss-type', 'Enterprise', 
    '--jboss-instances', '1', 
    '--ip-addresses', '127.0.0.1', 
    '--httpd-version', '2.2.15', 
    '--loadbalancer', 'mod_jk', 
    '--architecture', 'rhel5_64', 
    '--modjk-version', '1.2.30',
    '--sandbox-dir-name', 'testSandbox',
    '--archives-dir', dirArchives]

rhel64ModJkTwoInstancesEap500 = ['--jboss-version', '5.0.0', 
    '--jboss-type', 'Enterprise', 
    '--jboss-instances', '2', 
    '--ip-addresses', '127.0.0.1,127.0.0.2', 
    '--httpd-version', '2.2.15', 
    '--loadbalancer', 'mod_jk', 
    '--architecture', 'rhel5_64', 
    '--modjk-version', '1.2.30',
    '--sandbox-dir-name', 'testSandbox',
    '--archives-dir', dirArchives]



rhel64ModJkSuite = [rhel64ModJkOneInstanceEap501,
                    rhel64ModJkTwoInstancesEap501,
                    rhel64ModJkOneInstanceEap500, 
                    rhel64ModJkTwoInstancesEap500]

##################################################################################
# mod_cluster RHEL 64-bit
##################################################################################
rhel64ModClusterOneInstanceAdvertiseOnEap501 = ['--jboss-version', '5.0.1', 
    '--jboss-type', 'Enterprise', 
    '--jboss-instances', '1', 
    '--ip-addresses', '127.0.0.1', 
    '--httpd-version', '2.2.15', 
    '--loadbalancer', 'mod_cluster', 
    '--architecture', 'rhel5_64', 
    '--modcluster-advertise', 'true',
    '--sandbox-dir-name', 'testSandbox',
    '--archives-dir', dirArchives]

rhel64ModClusterOneInstanceAdvertiseOffEap501 = ['--jboss-version', '5.0.1', 
    '--jboss-type', 'Enterprise', 
    '--jboss-instances', '1', 
    '--ip-addresses', '127.0.0.1', 
    '--httpd-version', '2.2.15', 
    '--loadbalancer', 'mod_cluster', 
    '--architecture', 'rhel5_64', 
    '--modcluster-advertise', 'false',
    '--sandbox-dir-name', 'testSandbox',
    '--archives-dir', dirArchives]

rhel64ModClusterTwoInstancesAdvertiseOnEap501 = ['--jboss-version', '5.0.1', 
    '--jboss-type', 'Enterprise', 
    '--jboss-instances', '2', 
    '--ip-addresses', '127.0.0.1,127.0.0.2', 
    '--httpd-version', '2.2.15', 
    '--loadbalancer', 'mod_cluster', 
    '--architecture', 'rhel5_64', 
    '--modcluster-advertise', 'true',
    '--sandbox-dir-name', 'testSandbox',
    '--archives-dir', dirArchives]

rhel64ModClusterTwoInstancesAdvertiseOffEap501 = ['--jboss-version', '5.0.1', 
    '--jboss-type', 'Enterprise', 
    '--jboss-instances', '2', 
    '--ip-addresses', '127.0.0.1,127.0.0.2', 
    '--httpd-version', '2.2.15', 
    '--loadbalancer', 'mod_cluster', 
    '--architecture', 'rhel5_64', 
    '--modcluster-advertise', 'false',
    '--sandbox-dir-name', 'testSandbox',
    '--archives-dir', dirArchives]

rhel64ModClusterOneInstanceAdvertiseOnEap500 = ['--jboss-version', '5.0.0', 
    '--jboss-type', 'Enterprise', 
    '--jboss-instances', '1', 
    '--ip-addresses', '127.0.0.1', 
    '--httpd-version', '2.2.15', 
    '--loadbalancer', 'mod_cluster', 
    '--architecture', 'rhel5_64', 
    '--modcluster-advertise', 'true',
    '--sandbox-dir-name', 'testSandbox',
    '--archives-dir', dirArchives]

rhel64ModClusterOneInstanceAdvertiseOffEap500 = ['--jboss-version', '5.0.0', 
    '--jboss-type', 'Enterprise', 
    '--jboss-instances', '1', 
    '--ip-addresses', '127.0.0.1', 
    '--httpd-version', '2.2.15', 
    '--loadbalancer', 'mod_cluster', 
    '--architecture', 'rhel5_64', 
    '--modcluster-advertise', 'false',
    '--sandbox-dir-name', 'testSandbox',
    '--archives-dir', dirArchives]

rhel64ModClusterTwoInstancesAdvertiseOnEap500 = ['--jboss-version', '5.0.0', 
    '--jboss-type', 'Enterprise', 
    '--jboss-instances', '2', 
    '--ip-addresses', '127.0.0.1,127.0.0.2', 
    '--httpd-version', '2.2.15', 
    '--loadbalancer', 'mod_cluster', 
    '--architecture', 'rhel5_64', 
    '--modcluster-advertise', 'true',
    '--sandbox-dir-name', 'testSandbox',
    '--archives-dir', dirArchives]

rhel64ModClusterTwoInstancesAdvertiseOffEap500 = ['--jboss-version', '5.0.0', 
    '--jboss-type', 'Enterprise', 
    '--jboss-instances', '2', 
    '--ip-addresses', '127.0.0.1,127.0.0.2', 
    '--httpd-version', '2.2.15', 
    '--loadbalancer', 'mod_cluster', 
    '--architecture', 'rhel5_64', 
    '--modcluster-advertise', 'false',
    '--sandbox-dir-name', 'testSandbox',
    '--archives-dir', dirArchives]

rhel64ModClusterSuite = [rhel64ModClusterOneInstanceAdvertiseOnEap501,
                         rhel64ModClusterOneInstanceAdvertiseOffEap501,
                         rhel64ModClusterTwoInstancesAdvertiseOnEap501, 
                         rhel64ModClusterTwoInstancesAdvertiseOffEap501, 
                         rhel64ModClusterOneInstanceAdvertiseOnEap500,
                         rhel64ModClusterOneInstanceAdvertiseOffEap500,
                         rhel64ModClusterTwoInstancesAdvertiseOnEap500, 
                         rhel64ModClusterTwoInstancesAdvertiseOffEap500]


rhel64ModClusterOneInstanceAdvertiseOnEap501 = ['--jboss-version', '5.0.1', 
    '--jboss-type', 'Enterprise', 
    '--jboss-instances', '1', 
    '--ip-addresses', '127.0.0.1', 
    '--httpd-version', '2.2.15', 
    '--loadbalancer', 'mod_cluster', 
    '--architecture', 'rhel5_64', 
    '--modcluster-advertise', 'true',
    '--sandbox-dir-name', 'testSandbox',
    '--archives-dir', dirArchives]

linux32ModClusterSuite = []


rhel64GlobalQuickSuite = [rhel64ModClusterTwoInstancesAdvertiseOnEap501, 
                     rhel64ModJkTwoInstancesEap501, 
                     rhel64ModProxyTwoInstancesEap501]

rhel64GlobalAllSuite = []

for testOptions in rhel64ModProxySuite:
    rhel64GlobalAllSuite.append(testOptions)
for testOptions in rhel64ModJkSuite:
    rhel64GlobalAllSuite.append(testOptions)
for testOptions in rhel64ModClusterSuite:
    rhel64GlobalAllSuite.append(testOptions)

class TestApache(unittest.TestCase):

    def test_apache(self):
        basicOptions = ['--jboss-version', '5.0.1', 
                '--jboss-type', 'Enterprise', 
                '--jboss-instances', '1', 
                '--ip-addresses', '127.0.0.1', 
                '--httpd-version', '2.2.15', 
                '--loadbalancer', 'mod_cluster', 
                '--architecture', 'rhel5_64', 
                #'--modcluster-advertise', 'false',
                '--sandbox-dir-name', 'testSandbox']
        clusterConfig = ClusterConfig(basicOptions)
        clusterConfig.buildApache()
        clusterConfig.configureApache()
        clusterConfig.startApache()
        output = Utility.executeCmdAndReturnOutput("curl -S http://localhost/health.html")
        #print "curl output after Apache start: " + output
        self.assertTrue("Apache is up!" in output)
        clusterConfig.stopApache()
        output = Utility.executeCmdAndReturnOutput("curl -S http://localhost/health.html")
        #print "curl output after Apache stop: " + output
        self.assertTrue("(7)" in output or "(56)" in output)

class TestClusterConfig():

    def runTest(self, options):
        clusterConfig = ClusterConfig(options)
        clusterConfig.setupJBoss()
        clusterConfig.configureJBoss()
        clusterConfig.buildApache()
        clusterConfig.configureApache()
        clusterConfig.buildLoadbalancer()
        clusterConfig.configureLoadbalancer()
        try:
            clusterConfig.startApache()
            clusterConfig.startJBossProgrammatically()
            
            #Test that JBoss is up and running
            print "curling the counter app with command> curl -D /tmp/headers http://localhost/counter"
            curl = Utility.executeCmdAndReturnOutput("curl -D /tmp/headers http://localhost/counter")
            print "curl output: " + curl
            headers = Utility.executeCmdAndReturnOutput("cat /tmp/headers")
            print "headers: " + headers
            jsessionid = Utility.getJsessionidFromCurlHeaders(headers)
            time.sleep(4)
            output = self.curlCounterWithId(jsessionid)
            if "Counter =" in output and "Counter = 1" not in output:
                Utility.appendToFile(dirCurrent + "/report_" + timestamp + ".html", '<p><font style="color:green;">Success</font> -- Command> python clusterconfig.py ' + ' '.join(options) + '</p>')
            else:
                Utility.appendToFile(dirCurrent + "/report_" + timestamp + ".html", '<p><font style="color:red;">Failure</font> -- Command> python clusterconfig.py ' + ' '.join(options) + '</p>')

        finally:
            print "shutting down apache and JBoss"
            clusterConfig.stopApache()
            clusterConfig.stopJBossProgrammatically()
            
            
    def curlCounterWithId(self, jsessionid):
        print "jsessionid: %s", jsessionid
        print "curling the counter app with command>" + "curl --cookie %s http://localhost/counter", jsessionid
        output = Utility.executeCmdAndReturnOutput("curl --cookie " + jsessionid + " http://localhost/counter")
        print "Output: %s", output
        return output
    
def linuxRhel64Suite():
    tests = ['test_eap501mod_cluster_two_instances_advertise_off'
             'test_eap501mod_cluster_two_instances_advertise_off', 
             'test_eap500mod_cluster_two_instances_advertise_off',
             'test_eap501mod_cluster_two_instances_advertise_on',
             'test_eap500mod_cluster_two_instances_advertise_on']

    return unittest.TestSuite(map(TestClusterConfig, tests))


def test_main():
    
    testsuite = linuxRhel64Suite()
    runner = unittest.TextTestRunner(sys.stdout, verbosity=2)
    result = runner.run(testsuite)
    

if __name__ == "__main__":
    Utility.appendToFile(dirCurrent + "/report_" + timestamp + ".html", '<h1>Programatic test suite for clusterconfig.py</h1>')

    testClusterConfig = TestClusterConfig()
    ############################################################
    # Uncomment to run just one single test, if you do this
    # comment out the for loop below
    ############################################################
    testClusterConfig.runTest(rhel64ModProxyOneInstanceEap501)
    
    ############################################################
    # Available test suites
    ############################################################
    # rhel64ModClusterSuite, rhel64ModJkSuite, rhel64ModProxySuite, rhel64GlobalSuite, rhel64GlobalAllSuite
#    print "Test Suite: " + str(rhel64GlobalAllSuite)
#    for testOptions in rhel64GlobalAllSuite:
#        print "testOptions: " + str(testOptions)
#        testClusterConfig = TestClusterConfig()
#        testClusterConfig.runTest(testOptions)

    Utility.executeCmdAndReturnOutput("firefox " + dirCurrent + "/report_" + timestamp + ".html")
    












